$(document).ready(function() {
	var screenwidth, screenheight, mytop, getPosLeft, getPosTop
	screenwidth = $(window).width();
	screenheight = $(window).height();
	//获取滚动条距顶部的偏移
	mytop = $(document).scrollTop();
	//计算弹出层的left
	getPosLeft = screenwidth / 2 - 260;
	//计算弹出层的top
	getPosTop = screenheight / 2 - 150;
	//css定位弹出层
	$("#roleModifyDeptTree").css({ "left": getPosLeft, "top": getPosTop });
	$("#bathDeptTree").css({ "left": getPosLeft, "top": getPosTop });
	$("#deptTree").css({ "left": getPosLeft, "top": getPosTop });
	$("#updateAccount").css({ "left": getPosLeft, "top": getPosTop });
	$("#updateRole").css({ "left": getPosLeft, "top": getPosTop });
	$("#bathUpdateRole").css({ "left": getPosLeft, "top": getPosTop });
	//当浏览器窗口大小改变时
	$(window).resize(function() {
		screenwidth = $(window).width();
		screenheight = $(window).height();
		mytop = $(document).scrollTop();
		getPosLeft = screenwidth / 2 - 200;
		getPosTop = screenheight / 2 - 150;
		
		$("#roleModifyDeptTree").css({ "left": getPosLeft, "top": getPosTop+ mytop  });
		$("#bathDeptTree").css({ "left": getPosLeft, "top": getPosTop+ mytop  });
		$("#deptTree").css({ "left": getPosLeft, "top": getPosTop+ mytop  });
		$("#updateAccount").css({ "left": getPosLeft, "top": getPosTop+ mytop  });
		$("#updateRole").css({ "left": getPosLeft, "top": getPosTop+ mytop  });
		$("#bathUpdateRole").css({ "left": getPosLeft, "top": getPosTop+ mytop  });
	});
	//当拉动滚动条时，弹出层跟着移动
	$(window).scroll(function() {
		screenwidth = $(window).width();
		screenheight = $(window).height();
		mytop = $(document).scrollTop();
		getPosLeft = screenwidth / 2 - 200;
		getPosTop = screenheight / 2 - 150;
		
		$("#roleModifyDeptTree").css({ "left": getPosLeft, "top": getPosTop+ mytop  });	
		$("#bathDeptTree").css({ "left": getPosLeft, "top": getPosTop+ mytop  });	
		$("#deptTree").css({ "left": getPosLeft, "top": getPosTop+ mytop  });	
		$("#updateAccount").css({ "left": getPosLeft, "top": getPosTop+ mytop  });
		$("#updateRole").css({ "left": getPosLeft, "top": getPosTop+ mytop  });
		$("#bathUpdateRole").css({ "left": getPosLeft, "top": getPosTop+ mytop  });			 
	}); 
	 //获取页面文档的高度
	 var docheight = $(document).height();
	//追加一个层，使背景变灰
	$("body").append("<div id='greyBackGround'></div>");
	$("#greyBackGround").css({ "opacity": "0.1", "height": docheight });
	//$("#deptTreeBackGround").css({ "opacity": "0.1", "height": docheight });


	//条件查询		 
	$("#queryEmp").click(function(){
	 	$("#queryForm").submit();	
	});			 
	//清空			 		 
	$("#clean").click(function(){
		$("#empName")[0].value="";
		$("#email")[0].value="";
		$("#roleId").val("");
		$("#phone").val("");
		$("#queryDeptId").val("");
		$("#queryTempDeptId").val("");
	});
			 
	//账号管理应用按钮
	$("#updateAccountAppBtn").click(function(){
		var empno = $("#empno")[0].value;
	 	var loginName = $("#loginName")[0].value;
	 	if(loginName==null || loginName==""){
			alert("用户名不能为空!");
		 	return;
	 	}
		var password = $("#password")[0].value;
		if(password==null || password==""){
			alert("密码不能为空!");
			return;
		}
		$.ajax({
			type: "POST",
			url:"../../authority/employee/updateEmpAccount",
			data:"empno="+empno+"&loginName="+loginName+"&password="+password,
			async: false,
			error: function(request){
				$("#updateAccount").css("display","none");
				$("#greyBackGround").css("display","none");
				$("#loginName")[0].value="";
				$("#password")[0].value="";
	    		alert("Connection error");
	    		location.reload();
			},
			success: function(data){
				$("#updateAccount").css("display","none");
				$("#greyBackGround").css("display","none");
		 		$("#loginName")[0].value="";
		 		$("#password")[0].value="";
	 	 		alert("修改成功");
	 	 		location.reload();
	  		}
		}); 
	});
	//账号管理取消按钮
	$("#updateAccountBtnCancel").click(function(){
		$("#updateAccount").css("display","none");
		$("#greyBackGround").css("display","none");
		$("#loginName").attr("value","");
		$("#password").attr("value","");
	});
	//角色修改应用按钮
	$("#updateRoleAppBtn").click(function(){
		var deptId = $("#updateRoleDeptId")[0].value;
		if(deptId==null || deptId==""){
			alert("请选择部门!");
			return ;
	 	}
		var roleId = $("#updateRoleRoleId")[0].value;
		if(roleId==null || roleId==""){
			alert("请选择角色!");
			return ;
		}
		var isResponsible="";
		if($("#isResponsible").attr("checked")==true){
			isResponsible="1";
		}else{
			isResponsible="0";
		}
		var empno = $("#updateRoleEmpno")[0].value;
		 $.ajax({
			 type: "POST",
			 url:"../../authority/employee/updateEmpRole",
		     data:"empno="+empno+"&deptId="+deptId+"&roleId="+roleId+"&isResponsible="+isResponsible,
		     async: false,
		     error: function(request){
				$("#updateRole").css("display","none");
			 	$("#greyBackGround").css("display","none");
		 		$("#updateRoleDeptId")[0].value="";
		 		$("#updateRoleRoleId")[0].value="";
		 		$("#updateRoleEmpno")[0].value="";
		 		$("#isResponsible").attr("checked",false);
		        alert("Connection error");
		        location.reload();
		      },
			  success: function(data){
				$("#updateRole").css("display","none");
				$("#greyBackGround").css("display","none");
				$("#updateRoleDeptId")[0].value="";
				$("#updateRoleRoleId")[0].value="";
				$("#updateRoleEmpno")[0].value="";
				$("#isResponsible").attr("checked",false);
				alert("修改成功");
				location.reload();
		      }
		}); 
	});
	//角色修改取消按钮 
	 $("#updateRoleBtnCancel").click(function(){
		 $("#updateRole").css("display","none");
		 $("#greyBackGround").css("display","none");
		 $("#updateRoleDeptId").val("");
		 $("#updateRoleRoleId").val("");
		 $("#updateRoleEmpno")[0].value="";
	 });	 
	
	//批量角色修改应用按钮
	 $("#bathUpdateRoleAppBtn").click(function(){
		var empnoStr = $("#bathUpdateRoleEmpno")[0].value;
		if(empnoStr==null || empnoStr==""){
			alert("请选择要设置的员工!");
			return ;
		}
		var roleId = $("#bathUpdateRoleRoleId")[0].value;
		if(roleId==null || roleId==""){
			alert("请选择角色！");
			return;
		}
		var deptId = $("#bathUpdateRoleDeptId")[0].value;
		if(roleId==null || roleId==""){
			alert("请选择部门！");
			return;
		}
		 var isResponsible="";
		 if($("#bathUpdateRoleIsResponsible").attr("checked")==true){
			 isResponsible="1";
		 }else{
			 isResponsible="0";
		 }
		 $.ajax({
	            type: "POST",
	            url:"../../authority/employee/bathUpdateEmpRole",
	            data:"empnoStr="+empnoStr+"&deptId="+deptId+"&roleId="+roleId+"&isResponsible="+isResponsible,
	            async: false,
	            error: function(request) {
	       		 $("#bathUpdateRole").css("display","none");
	       		 $("#greyBackGround").css("display","none");
	         		 $("#bathUpdateRoleDeptId")[0].value="";
	         		 $("#bathUpdateRoleRoleId")[0].value="";
	         		 $("#bathUpdateRoleEmpno")[0].value="";
	         		 $("#bathUpdateRoleIsResponsible").attr("checked",false);
	                alert("Connection error");
	                location.reload();
	            },
	            success: function(data) {
	       		 $("#bathUpdateRole").css("display","none");
	       		 $("#greyBackGround").css("display","none");
	         		 $("#bathUpdateRoleDeptId")[0].value="";
	         		 $("#bathUpdateRoleRoleId")[0].value="";
	         		 $("#bathUpdateRoleEmpno")[0].value="";
	         		 $("#bathUpdateRoleIsResponsible").attr("checked",false);
	           	 alert("修改成功");
	           	 location.reload();
	            }
	        });	
	 });
	//批量角色修改取消按钮	 
	 $("#bathUpdateRoleBtnCancel").click(function(){
		 	$("#bathUpdateRole").css("display","none");
			$("#greyBackGround").css("display","none");
			$("#bathUpdateRoleDeptId").val("");
			$("#bathUpdateRoleRoleId").val("");
			$("#bathUpdateRoleEmpno")[0].value="";
			$("#bathUpdateRoleIsResponsible").attr("checked",false); 
	 });	
	
	 //批量角色修改按钮	
	 $("#bathUapteRole").click(function(){
		 var list = $("input[id^=empId_][type='checkbox']:checked");
		 if(list.length=="0"){
			 alert("请选择要批量设置的员工!");
			 return;
		 }
		 var empnoStr = "";
		 for ( var int = 0; int < list.length; int++) {
			 var empId =  list[int].value;
			 if(int==0){
				 empnoStr=empId;
			 }else{
				 empnoStr=empnoStr+","+empId; 
			 }	
		}
		$("#bathUpdateRoleEmpno")[0].value = empnoStr;
		$("#greyBackGround").css("display","block");
		$("#bathUpdateRole").fadeIn("slow"); 
	 });
	 

	 $("#deptTreeBtnCancel").click(function(){
		 	$("#greyBackGround").css("display","none");
		 	$("#deptTree").css("display","none");
	});
	$("#queryDeptTreeBtnConfirm").click(function(){
		var selectedNodeId="";
		var selectedNodeName="";
        var treeObj=$.fn.zTree.getZTreeObj("treeDemo");
        nodes=treeObj.getSelectedNodes();
		 for ( var int = 0; int < nodes.length; int++){
			 var node = nodes[int];
			 selectedNodeId = node.id;
			 selectedNodeName = node.name;
			 break;
		} 
        if(selectedNodeId==""){
        	alert("请选择部门!");
        	return;
        }
		$("#queryDeptId").val(selectedNodeId);
		$("#queryTempDeptId").val(selectedNodeName);
	 	$("#greyBackGround").css("display","none");
	 	$("#deptTree").css("display","none");
	});
		
	
	 $("#bathDeptTreeBtnCancel").click(function(){
		 	$("#greyBackGround").css("display","none");
		 	$("#bathDeptTree").css("display","none");
	});
	$("#bathQueryDeptTreeBtnConfirm").click(function(){
		var selectedNodeId="";
		var selectedNodeName="";
	    var treeObj=$.fn.zTree.getZTreeObj("bathDeptTreeDemo");
	    nodes=treeObj.getSelectedNodes();
		 for ( var int = 0; int < nodes.length; int++){
			 var node = nodes[int];
			 selectedNodeId = node.id;
			 selectedNodeName = node.name;
			 break;
		} 
	    if(selectedNodeId==""){
	    	alert("请选择部门!");
	    	return;
	    }
		$("#bathUpdateRoleDeptId").val(selectedNodeId);
		$("#bathQueryTempDeptId").val(selectedNodeName);
	 	$("#greyBackGround").css("display","none");
	 	$("#bathDeptTree").css("display","none");
	});
	

	 $("#roleModifyDeptTreeBtnCancel").click(function(){
		 	$("#greyBackGround").css("display","none");
		 	$("#roleModifyDeptTree").css("display","none");
	});
	$("#roleModifyQueryDeptTreeBtnConfirm").click(function(){
		var selectedNodeId="";
		var selectedNodeName="";
	    var treeObj=$.fn.zTree.getZTreeObj("roleModifyDeptTreeDemo");
	    nodes=treeObj.getSelectedNodes();
		 for ( var int = 0; int < nodes.length; int++){
			 var node = nodes[int];
			 selectedNodeId = node.id;
			 selectedNodeName = node.name;
			 break;
		} 
	    if(selectedNodeId==""){
	    	alert("请选择部门!");
	    	return;
	    }
		$("#updateRoleDeptId").val(selectedNodeId);
		$("#roleModifyQueryTempDeptId").val(selectedNodeName);
	 	$("#greyBackGround").css("display","none");
	 	$("#roleModifyDeptTree").css("display","none");
	});
			
			
	var setting = {	
			view: {selectedMulti:false},data: {keep: {parent:true,leaf:true},simpleData: {enable: true}},
			callback: {beforeDrag: beforeDrag}
	};
	function beforeDrag(treeId, treeNodes){
		return false;
	}
	$.fn.zTree.init($("#treeDemo"), setting, zNodes);
	$.fn.zTree.init($("#bathDeptTreeDemo"), setting, zNodes);
	$.fn.zTree.init($("#roleModifyDeptTreeDemo"), setting, zNodes);
});
		
function accountMangment(empno){
	$("#greyBackGround").css("display","block");
	$("#updateAccount").fadeIn("slow");
	$("#empno")[0].value=empno;
}

function roleModify(empno,deptId,roleId,isResponsible,deptName){
	 $("#updateRoleDeptId").val(deptId);
	 $("#roleModifyQueryTempDeptId").val(deptName);
	 $("#updateRoleRoleId").val(roleId);
	 if(isResponsible=="1"){
		 $("#isResponsible").attr("checked",true);
		 $("#isResponsible").value="1";
		
	 }else{
		 $("#isResponsible").attr("checked",false); 
		 $("#isResponsible").value="0";
		 
	 }
	 $("#greyBackGround").css("display","block");
	 $("#updateRole").fadeIn("slow");
	 $("#updateRoleEmpno")[0].value=empno;
}

function getEmpList(pageNow){
	$("#pageNow")[0].value=pageNow;
	$("#queryForm").submit();
}

function queryDeptTree(){
	 $("#queryDeptTreeBtnConfirm").css("display","block");
	 $("#greyBackGround").css("display","block");
	 $("#deptTree").fadeIn("slow"); 
}

function bathQueryDeptTree(){
	 $("#bathQueryDeptTreeBtnConfirm").css("display","block");
	 $("#greyBackGround").css("display","block");
	 $("#bathDeptTree").fadeIn("slow"); 
}

function roleModifyQueryDeptTree(){
	 $("#roleModifyQueryDeptTreeBtnConfirm").css("display","block");
	 $("#greyBackGround").css("display","block");
	 $("#roleModifyDeptTree").fadeIn("slow"); 
}

function selectAll(){
	if($("#selectAll").attr("checked")==true){
		$('input[id^=empId_]').attr("checked",true); 
	}else{
		$('input[id^=empId_]').attr("checked",false); 
		
	}
}




